package com.ray.project.seismograph;

import java.util.Timer;
import java.util.TimerTask;

import com.ray.project.oceanicwaveinformation.R;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class Seismograph extends Activity {
  SensorManager sensorManager;
  
  TextView currentxTextView;
  TextView currentyTextView;
  TextView currentzTextView;
  TextView richterScaleTextView;
  
  float currentx = 0;
  float currenty = 0;
  float currentz = 0;
  
	
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.seismograph_activity);
  
    currentxTextView = (TextView)findViewById(R.id.xvalue);
    currentyTextView = (TextView)findViewById(R.id.yvalue);
    currentzTextView = (TextView)findViewById(R.id.zvalue);
    richterScaleTextView = (TextView)findViewById(R.id.richter_scale);
    
    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

    Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(sensorEventListener,
                                   accelerometer,
                                   SensorManager.SENSOR_DELAY_FASTEST);
    
    Timer updateTimer = new Timer("gForceUpdate");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
        updateGUI();
      }
    }, 0, 2000);
  }
  
  private final SensorEventListener sensorEventListener = new SensorEventListener() {
    double gravity = SensorManager.STANDARD_GRAVITY;

    public void onAccuracyChanged(Sensor sensor, int accuracy) { }

    public void onSensorChanged(SensorEvent event) {
      double xAxis = event.values[0];
      double yAxis = event.values[1];
      double zAxis = event.values[2];

      double a = Math.round(Math.sqrt(Math.pow(xAxis, 2) + 
                                      Math.pow(yAxis, 2) + 
                                      Math.pow(zAxis, 2)));
      //currentAcceleration = Math.abs((float)(a-gravity));
      
      currentx = Math.abs((float)(xAxis-gravity));
      currenty = Math.abs((float)(yAxis-gravity));
      currentz = Math.abs((float)(zAxis-gravity));
      
  }};
  
  private void updateGUI() {
    runOnUiThread(new Runnable() {
      public void run() {
        String currentX = currentx/SensorManager.STANDARD_GRAVITY + "Gs";
        String currentY = currenty/SensorManager.STANDARD_GRAVITY + "Gs";
        String currentZ = currentz/SensorManager.STANDARD_GRAVITY + "Gs";
        
     
        
        currentxTextView.setText(currentX);
        currentxTextView.invalidate();

        currentyTextView.setText(currentY);
        currentyTextView.invalidate();
        
        currentzTextView.setText(currentZ);
        currentzTextView.invalidate();
        
        if (currentx/SensorManager.STANDARD_GRAVITY < 0.1  || currenty/SensorManager.STANDARD_GRAVITY < 0.1 || currenty/SensorManager.STANDARD_GRAVITY < 0.1)
        {
        	richterScaleTextView.setText("Micro");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 0.2  || currenty/SensorManager.STANDARD_GRAVITY < 0.2 || currenty/SensorManager.STANDARD_GRAVITY < 0.2)
        {
        	richterScaleTextView.setText("Minor");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 0.3  || currenty/SensorManager.STANDARD_GRAVITY < 0.3 || currenty/SensorManager.STANDARD_GRAVITY < 0.3)
        {
        	richterScaleTextView.setText("Light");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 0.6  || currenty/SensorManager.STANDARD_GRAVITY < 0.6 || currenty/SensorManager.STANDARD_GRAVITY < 0.6)
        {
        	richterScaleTextView.setText("Moderate");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 0.9  || currenty/SensorManager.STANDARD_GRAVITY < 0.9 || currenty/SensorManager.STANDARD_GRAVITY < 0.9)
        {
        	richterScaleTextView.setText("Strong");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 1.3  || currenty/SensorManager.STANDARD_GRAVITY < 1.3 || currenty/SensorManager.STANDARD_GRAVITY < 1.3)
        {
        	richterScaleTextView.setText("Major");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY < 1.8  || currenty/SensorManager.STANDARD_GRAVITY < 1.8 || currenty/SensorManager.STANDARD_GRAVITY < 1.8)
        {
        	richterScaleTextView.setText("Great");
        	richterScaleTextView.invalidate();
        }
        else if (currentx/SensorManager.STANDARD_GRAVITY > 1.8  || currenty/SensorManager.STANDARD_GRAVITY > 1.8 || currenty/SensorManager.STANDARD_GRAVITY > 1.8)
        {
        	richterScaleTextView.setText("Massive");
        	richterScaleTextView.invalidate();
        }   
      }
    });
  };
}